package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yu.crm.domain.pojo.CrmPointPatrol;
import com.yu.crm.domain.pojo.CrmRoutePatrol;
import com.yu.crm.domain.pojo.CrmRouteUser;
import com.yu.crm.mapper.CrmRoutePatrolMapper;
import com.yu.crm.service.ICrmRoutePatrolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzz.common.config.UserCacheManager;
import com.yxzz.common.helper.DataHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 巡更路线 服务实现类
 * </p>
 *
 * @author pan
 * @since 2020-09-23
 */
@Service
public class CrmRoutePatrolService extends ServiceImpl<CrmRoutePatrolMapper, CrmRoutePatrol> implements ICrmRoutePatrolService {

    @Autowired
    private CrmPointPatrolService pointPatrolService;
    @Autowired
    private UserCacheManager userCacheManager;
    @Autowired
    private CrmRouteUserService routeUserService;

    @Override
    public void delete(Long id) {
        List<CrmPointPatrol> list = pointPatrolService.findByRoutePatrolId(id);
        if(DataHelpers.isNotEmpty(list) && list.size()>0){
            List<Long> pointPatrolIds = list.stream().map(v -> v.getId()).collect(Collectors.toList());
            pointPatrolService.removeByIds(pointPatrolIds);
            this.removeById(id);
        }
    }

    @Override
    public CrmRoutePatrol detail(Long id) {
        CrmRoutePatrol routePatrol = this.getById(id);
        List<CrmPointPatrol> list = pointPatrolService.findByRoutePatrolId(id);
        routePatrol.setPointPatrols(list);
        return routePatrol;
    }

    @Override
    public CrmRoutePatrol detail() {
        CrmRoutePatrol crmRoutePatrol = new CrmRoutePatrol();
        String userId = userCacheManager.getCurrentUser().getUserId();
        return baseMapper.detail(userId);
    }

}
